package com.zhentao.util;

import org.apache.http.HttpResponse;

import java.util.HashMap;
import java.util.Map;

// 短信发送工具类
public class SmsSendUtil {

    // 发送短信的静态方法
    public static boolean sendSms(String mobile, String coders) {
        String host = "https://gyytz.market.alicloudapi.com";
        String path = "/sms/smsSend";
        String method = "POST";

        Map<String, String> headers = new HashMap<>();
        headers.put("Authorization", "APPCODE " + "1bd3d4d411d048209db3d9a5cfedff48");

        Map<String, String> querys = new HashMap<>();
        querys.put("mobile", mobile);
        // 将生成的验证码填充到 param 参数中
        querys.put("param", "**code**:" + coders + ",**minute**:5");
        querys.put("smsSignId", "2e65b1bb3d054466b82f0c9d125465e2");
        querys.put("templateId", "908e94ccf08b4476ba6c876d13f084ad");

        Map<String, String> bodys = new HashMap<>();

        try {
            // 调用 HttpUtils 类的 doPost 方法发送请求
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            // 检查响应状态码，判断短信是否发送成功
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == 200) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
